In Kotlin, nullability is a part of the type system. By default, any given type T
is non-nullable. If you append a "?" to the type, it
becomes nullable: T?
.
When accessing properties or functions of a nullable type, you need to handle the case when the target is null
. However, while
accessing a non-nullable type, it is redundant to test for null
, as the compiler statically ensures that the value can never be
null
. So all the nullability checks on the non-nullable types are considered code smells.
On the other hand, performing a null-check on a value that is always null is equally as redundant.
Here is an example of a non-nullable variable. s
is of a type String
and cannot be null
.
val s: String = ""
Here is an example of a nullable variable. Nullable variables are declared by using the ?
.
val s: String? = null
Explicit null checks are comparing a result to null
using ==
or !=
operators. In Kotlin, there are various
other means of implicitly or explicitly performing a null check or assertion, including the following:
- Safe call operator
?.
- Elvis operator
?:
- Not-null assertion operator
!!
-
requireNotNull
and checkNotNull
functions